package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.xiaomi.entity.Manager;
import com.xiaomi.model.Result;
import com.xiaomi.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ManagerController
 * @Description TODO
 * @Author zcy
 * @Date 2021/11/29 11:21
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/manager",produces = "application/json;charset=utf-8")
public class ManagerController {
    @Autowired
    private ManagerService managerService;
    @GetMapping("/list.do")
    @ResponseBody
    public String tuijian(@RequestParam Integer page, @RequestParam Integer limit,
                          @RequestParam(required = false) String managerName, @RequestParam(required = false) Integer managerId){
        Integer startRow = (page-1)*limit;
        List<Manager> list = managerService.pageSearch(startRow,limit,managerId,managerName);
        Integer count = managerService.getCount(managerId,managerName);
        Result result = new Result();
        result.setData(list);
        result.setCode(0);
        result.setCount(count);
        String json = JSON.toJSONString(result);
        return json;
    }
    @PostMapping("/add.do")
    @ResponseBody
    public  String insert(Manager manager){

        managerService.insert(manager);


        Result result = new Result();
        result.setCode(0);
        result.setMsg("添加成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @PostMapping("/update.do")
    @ResponseBody
    public  String update(Manager manager){
        managerService.update(manager);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("修改成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @PostMapping("/del.do")
    @ResponseBody
    public String delete( @RequestParam Integer managerId){
        managerService.delete(managerId);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("删除成功");
        String json = JSON.toJSONString(result);
        return json;
    }
}
